﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        private static DataTable data = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                FillDate();
            }

        }

        private void FillDate()
        {
            var sql = "select * from Student";
            data = DbHelper.GetData(sql);
            GridView1.DataSource = data;
            GridView1.DataBind();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillDate();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillDate();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 0);
            var sql = string.Format("delete from Student where id='{0}'", id);
            DbHelper.AddOrUpdateOrDelete(sql);
            GridView1.EditIndex = -1;
            FillDate();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetCellValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);
            var name = GetCellValue(e.RowIndex, 1);
            var sex = GetCellValue(e.RowIndex, 2);
            var remark = GetCellValue(e.RowIndex, 3);
            if (isOk)
            {
                var sql = string.Format("update Student set StuName='{0}',StuSex='{1}',Remark='{2}'where Id='{3}'", name, sex, remark, id);
                DbHelper.AddOrUpdateOrDelete(sql);
               
            }
            else
            {
                var sql = string.Format("insert into Student(StuName,StuSex,Remark)values('{0}','{1}','{2}')", name, sex, remark);
                DbHelper.AddOrUpdateOrDelete(sql);
            }

          
            GridView1.EditIndex = -1;
            FillDate();
        }

        private string GetCellValue(int rowIndex, int v)
        {
            var control = GridView1.Rows[rowIndex].Cells[v];
            var str = control.Controls.Count>0?((TextBox)control.Controls[0]).Text:control.Text;
            return str;
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            //DataTable table = new DataTable();
            //table.Columns.Add("Id");
            //table.Columns.Add("StuName");
            //table.Columns.Add("StuSex");
            //table.Columns.Add("Remark");
            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    var newRow = table.NewRow();
            //    newRow["Id"] = GetCellValue(row.RowIndex, 0);
            //    newRow["StuName"] = GetCellValue(row.RowIndex, 1).Replace("&nbsp;", " ");
            //    newRow["StuSex"] = GetCellValue(row.RowIndex, 2).Replace("&nbsp;", " ");
            //    newRow["Remark"] = GetCellValue(row.RowIndex, 3).Replace("&nbsp;", " ");
            //    table.Rows.Add(newRow);
            //}
            //table.Rows.Add(table.NewRow());
            //GridView1.DataSource = table;
            //GridView1.DataBind();
            //GridView1.EditIndex = table.Rows.Count - 1;
            //GridView1.DataSource = table;
            //GridView1.DataBind();
            var dt= data.Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        } 
    }
}